#!/bin/sh
# the next line restarts using wish \
exec wish "$0" "$@"

#données globales
set base_default_left_width 180
set base_default_right_width 450
set base_default_height 400

set salons_users_default_width 170
set salons_users_default_height 150

set text_default_width $base_default_right_width
set text_default_height 80

set chat_default_width $base_default_right_width
set chat_default_height [expr {$base_default_height - $text_default_height}]

set debug_default_width $salons_users_default_width
set debug_default_height $base_default_height


global listeSalons
global salonsList



# Chemin pour trouver les differents fichers
set var_config_cheminAbsolu [info script] 
set var_config_cheminAbsolu [file dirname $var_config_cheminAbsolu] 

# Ou chercher les packages
lappend auto_path [file join $var_config_cheminAbsolu packages] 

#package tile, doit se trouver dans le repertoire packages
package require Tk
package require tile
#choix du theme
tile::setTheme clam



#font et styles...
set titles_font "Mono 12 bold"
#changement de la font des boutons
style default TButton \
	-font $titles_font \
	;


#paned horizontal (principal)
variable HORIZONTAL_PANED [ttk::paned .hp -orient horizontal]

#frame de base...
variable F_LEFT_BASE [ttk::frame "$HORIZONTAL_PANED.left_base" -relief flat -width $base_default_left_width -height $base_default_height -padding 5]
variable F_RIGHT_BASE [ttk::frame "$HORIZONTAL_PANED.right_base" -relief flat -width $base_default_right_width -height $base_default_height -padding 5]
#frame pour debug
variable F_EXTREM_RIGHT_DEBUG_BASE [ttk::frame "$HORIZONTAL_PANED.extrem_right_base" -relief flat -width $base_default_right_width -height $base_default_height -padding 5]

#paned gauche (salons - users)
variable LEFT_SU_PANED [ttk::paned "$F_LEFT_BASE.lp"]
#paned droit (salon - saisie)
variable LEFT_SS_PANED [ttk::paned "$F_RIGHT_BASE.rp"]


#labelframe des SALONS...
variable F_SALONS_title [ttk::label "$LEFT_SU_PANED.salons_title" -text " Salons " -font $titles_font]
variable F_SALONS [ttk::labelframe "$LEFT_SU_PANED.salons" -width $salons_users_default_width -height $salons_users_default_height -padding 5 -labelwidget $F_SALONS_title]
#labelframe des USERS...
variable F_USERS_title [ttk::label "$LEFT_SU_PANED.users_title" -text " Utilisateurs " -font $titles_font]
variable F_USERS [ttk::labelframe "$LEFT_SU_PANED.users" -width $salons_users_default_width -height $salons_users_default_height -padding 5 -labelwidget $F_USERS_title]


#labelframe du CHAT...
variable F_CHAT_title [ttk::label "$LEFT_SS_PANED.chat_title" -text " Chat " -font $titles_font]
variable F_CHAT [ttk::labelframe "$LEFT_SS_PANED.chat" -width $chat_default_width -height $chat_default_height -padding 5 -labelwidget $F_CHAT_title]


#labelframe du DEBUG...
variable F_DEBUG_title [ttk::label "$F_EXTREM_RIGHT_DEBUG_BASE.chat_title" -text " Debug " -font $titles_font]
variable F_DEBUG [ttk::labelframe "$F_EXTREM_RIGHT_DEBUG_BASE.chat" -width $debug_default_width -height $debug_default_height -padding 5 -labelwidget $F_DEBUG_title]


#frame du TEXTE...
variable F_TEXT [ttk::frame "$LEFT_SS_PANED.text" -width $text_default_width -height $text_default_height -padding 5]

#entry et button send
variable TEXT_ENTRY [ttk::entry "$F_RIGHT_BASE.entry"]
variable SEND_BUTTON [ttk::button "$F_RIGHT_BASE.send" -width 6  -text envoyer -command proctest]



#Construire un treeview
proc buildTree {w} {
	variable TV [ttk::frame "$w"]
	ttk::scrollbar $TV.vsb -command [list $TV.t yview]
	ttk::treeview $TV.t -show tree \
		-padding 0 \
		-yscrollcommand [list sbset $TV.vsb] 

	grid $TV.t $TV.vsb -sticky nwse
	grid columnconfigure $TV 0 -weight 1
	grid rowconfigure $TV 0 -weight 1
	grid propagate $TV 0
	return $w
}

#remplir un treeview
proc fillTree {TV} {
	$TV.t insert {} 0 -id a -text "agggggggggggggggggggg" -open 0
	$TV.t insert {} 0 -id b -text "b" -open 0
	$TV.t insert {} 0 -id c -text "c" -open 0
	$TV.t insert {} 0 -id d -text "d" -open 0
	$TV.t insert {} 0 -id e -text "e" -open 0
	$TV.t insert {} 0 -id f -text "f" -open 0
}
variable salonsList [list a b c d e f]



## sbset $sb -- auto-hide scrollbar 
#	Scrollable widget -[xy]scrollcommand prefix.
#	Sets the scrollbar, auto-hides/shows.
#	Scrollbar must be controlled by the grid geometry manager.
#
proc sbset {sb first last} {
    if {$first <= 0 && $last >= 1} {
    	grid remove $sb
    } else {	
        grid $sb
    }
    $sb set $first $last
}

#end test


 #gestion des paned
$LEFT_SU_PANED add $F_SALONS -weight 1;
$LEFT_SU_PANED add $F_USERS -weight 1;

$LEFT_SS_PANED add $F_CHAT -weight 1;
$LEFT_SS_PANED add $F_TEXT -weight 0;

pack $LEFT_SS_PANED -in $F_RIGHT_BASE -side top -expand true -fill both

$HORIZONTAL_PANED add $F_LEFT_BASE  -weight 0;
$HORIZONTAL_PANED add $F_RIGHT_BASE -weight 1;
$HORIZONTAL_PANED add $F_EXTREM_RIGHT_DEBUG_BASE  -weight 0;
#fin gestion des paned

#geometry manager
pack $HORIZONTAL_PANED -side right -expand true -fill both


# test listeSalons
variable listeSalons [buildTree "$F_SALONS.listeSalons"]
fillTree $listeSalons
proc proctest {} {
	global listeSalons
	global salonsList
	$listeSalons.t delete $salonsList
}
# test listeUsers
variable listeUsers [buildTree "$F_USERS.listeUsers"]
#fillTree $listeUsers


#principaux labelframes
pack $listeSalons -in $F_SALONS -side top -expand true -fill both
pack $listeUsers -in $F_USERS -side top -expand true -fill both
#pack $F_USERS -in $F_LEFT_BASE -side top -expand true -fill both

pack $LEFT_SU_PANED -in $F_LEFT_BASE -side top -expand true -fill both

#end test
# test pour les notebooks
variable NB_SALON [ttk::notebook "$F_CHAT.notebook" -width $chat_default_width -height $chat_default_height]
set pw [ttk::paned $NB_SALON.client -orient horizontal]
$NB_SALON add $pw -text "Demo" -underline 0 -padding 6

pack $NB_SALON -in $F_CHAT -side top -expand true -fill both

pack $F_DEBUG -in $F_EXTREM_RIGHT_DEBUG_BASE -side top -expand true -fill both

pack $TEXT_ENTRY -in $F_TEXT -side left -expand true -fill both
pack $SEND_BUTTON -in $F_TEXT -side right -expand false -fill both

#update
#puts [winfo width .hp]
#puts [winfo width .hp]